package algorithm.Test;

import java.util.Arrays;

import static algorithm.Common.SimulateJudge.copyArray;
import static algorithm.Common.SimulateJudge.randomArray;
import static algorithm.Sort.BinarySort.binarySortFindMostRight;

public class BinarySortTest {
    public void binary_sort() {
        //N代表数组的最大长度
        int N = 100;
        //v代表取值范围
        int v = 1000;
        //test_times代表测试次数
        int testTimes = 50000;

        System.out.println("测试开始");
        for (int i = 0; i < testTimes; i++) {
            //准备测试数据
            int n = (int) (Math.random() * N);
            int[] arr = randomArray(n, v);
            Arrays.sort(arr);
            int[] arr1 = copyArray(arr);
            int[] arr2 = copyArray(arr);
            int num = (int) (Math.random() * v);

            //开始测试
//            boolean flag1= binarySortFindNum(arr1,num);
//            boolean flag2=bruteFindNum(arr2,num);

//            int flag1=binarySortFindMostLeft(arr1,num);
//            int flag2=bruteFindMostLeft(arr2,num);

            int flag1 = binarySortFindMostRight(arr1, num);
            int flag2 = bruteFindMostRight(arr2, num);
            if (flag1 != flag2) {
                System.out.println("测试出错");
                //可以在此打印错误信息
            }
        }
        System.out.println("测试结束");
    }

    private static boolean bruteFindNum(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) return true;
        }
        return false;
    }

    private static int bruteFindMostLeft(int[] arr, int num) {
        int ans = -1;
        for (int i = arr.length - 1; i >= 0 && arr[i] >= num; i--) {
            ans = i;
        }
        return ans;

    }

    private static int bruteFindMostRight(int[] arr, int num) {
        int ans = -1;
        for (int i = 0; i < arr.length && arr[i] <= num; i++) {
            ans = i;
        }
        return ans;

    }
}
